package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.CursoDAORemote;
import proyecto.EntityBeans.Curso;


@Stateless
@Remote(CursoBLRemote.class)
public class CursoBL implements CursoBLRemote {
	CursoDAORemote cursoDAORemote;
	
	public int save(Curso curso) {	
		
		try
		{
			Context context = new InitialContext();
			cursoDAORemote = (CursoDAORemote) context.lookup("CursoDAO/remote");
			
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}					
		
		return cursoDAORemote.save(curso);
		
	}
		
	public Curso findById(int id) {
		try
		{
			Context context = new InitialContext();
			cursoDAORemote = (CursoDAORemote) context.lookup("CursoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return cursoDAORemote.findById(id);
		
	}
	
		
	public void merge(Curso curso) {
		try
		{
			Context context = new InitialContext();
			cursoDAORemote = (CursoDAORemote) context.lookup("CursoDAO/remote");
			cursoDAORemote.merge(curso);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	
	public List findByNameCompleto(String name)
	{	
		try
		{
			Context context = new InitialContext();
			cursoDAORemote = (CursoDAORemote) context.lookup("CursoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return cursoDAORemote.findByNameCompleto(name);
	}
	
	
	public List findByName(String name)
	{	
		try
		{
			Context context = new InitialContext();
			cursoDAORemote = (CursoDAORemote) context.lookup("CursoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return cursoDAORemote.findByName(name);
		
	}
	
	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			cursoDAORemote = (CursoDAORemote) context.lookup("CursoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return cursoDAORemote.findAll();
	}
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			cursoDAORemote = (CursoDAORemote) context.lookup("CursoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return cursoDAORemote.findByIdList(id);
	}
	
		
	public void remove(int idCurso){	
		try
		{
			Context context = new InitialContext();
			cursoDAORemote = (CursoDAORemote) context.lookup("CursoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
				
		cursoDAORemote.remove(idCurso);
	}
	
	public void refresh(Curso curso){
		try
		{
			Context context = new InitialContext();
			cursoDAORemote = (CursoDAORemote) context.lookup("CursoDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		cursoDAORemote.refresh(curso);
	}
}
